﻿using System.Web;
using System.Web.Configuration;

namespace PhantomDb.Config.Web
{
    public class PhantomWebConfig: AbstractConfigurator
    {
        public override string ParseConnectionString(string connection)
        {
            string result = base.ParseConnectionString(connection);

            if(result.Contains("~/")) {
                
                int start = result.IndexOf("~/");
                int end = result.IndexOf(";", start);

                string url;

                if (end == -1)
                {
                    url = result;
                }
                else
                {
                    url = result.Substring(start, end - start);
                }

                string path = HttpContext.Current.Server.MapPath(url);

                result = result.Replace(url, path);
            }

            return result;
        }

        protected override Configuration.Phantom GetSection()
        {
            var section = WebConfigurationManager.GetSection("Phantom") as Configuration.Phantom;
            return section;
        }

        protected override void Save(Core.Environment e)
        {
            PhantomConfig.E = e;
        }
    }
}
